package de.hhbk.feedbackmodul.converter;

import java.util.List;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;

import de.hhbk.feedbackmodul.beans.applicationscope.ApplicationManagerBean;
import de.hhbk.feedbackmodul.entities.Question;

public class QuestionConverter implements Converter {
	
	@Override
	public Object getAsObject(FacesContext fc, UIComponent comp, String value) {
		List<Question> list = getQuestionList(fc);
		for(Question ap: list)
		{
			if(ap.getText().equals(value))
			{
				return ap;
			}
		}
		return null;
	}

	@Override
	public String getAsString(FacesContext arg0, UIComponent arg1, Object o) {
		if(o == null)
		{
			return "";
		}
		else
		{
			return ((Question)o).getText();
		}
	}
	
	private List<Question> getQuestionList(FacesContext facesContext) {
            ApplicationManagerBean masterBean= (ApplicationManagerBean) facesContext.getExternalContext().getApplicationMap().get("applicationManagerBean");
        return masterBean.getQuestionList();
    }
}
